package com.ktv.web.controller;

import com.ktv.web.controller.com.ktv.web.handler.RequestHandler;
import com.ktv.web.controller.com.ktv.web.handler.impl.AsyncRequestHandler;
import com.ktv.web.controller.com.ktv.web.handler.impl.SimpleAjaxRequestHandler;
import com.ktv.web.controller.com.ktv.web.handler.impl.SteamRequestHandler;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by icey on 2016/1/16.
 */

@Controller
@RequestMapping("ktv")
public class BaseController {


    @Resource
    ApplicationContext applicationContext;

    @PostConstruct
    public void init() {
        handlerMap = new HashMap<>(4);
        handlerMap.put("Ajax", applicationContext.getBean(SimpleAjaxRequestHandler.class));
        handlerMap.put("Steam", applicationContext.getBean(SteamRequestHandler.class));
        handlerMap.put("Long", applicationContext.getBean(AsyncRequestHandler.class));
     //   handlerMap.put("Socket", applicationContext.getBean(AsyncRequestHandler.class));
    }

    Map<String, RequestHandler> handlerMap;

    @RequestMapping({"index", "index.json"})
    public String index() {
        System.out.println("Aaaaaaaaaaaaaaa");
        return "index";
    }


    @RequestMapping({"{type}/time"})
    public String keepLive(@PathVariable String type, HttpServletRequest request, HttpServletResponse response) {

        if (StringUtils.equals(type, "index")) {
            return "index";
        }
        try {
           return handlerMap.get(type).handle(request, response);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "index";
    }
}
